<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator="/" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">数据统计</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick">实训操作记录</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <div class="left-title flex"></div>
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-cascader v-model="treeId" placeholder="请选择考场组织" :options="schoolArray" :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'nextDatas',
              }" @change="changeTreeItem" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.deviceId" placeholder="请选择设备名称" @change="onSubmit" clearable>
              <el-option v-for="item in deviceArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.type" placeholder="请选择实训类型" @change="onSubmit" clearable>
              <el-option label="学生" value="0"></el-option>
              <el-option label="老师" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker v-model="date" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" @change="handleDate" clearable>
            </el-date-picker>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.status" placeholder="请选择实训状态" @change="onSubmit" clearable>
              <el-option label="进行中" value="0"></el-option>
              <el-option label="已完成" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.commentStatus" placeholder="请选择批阅状态" @change="onSubmit" clearable>
              <el-option label="未批阅" value="0"></el-option>
              <el-option label="已批阅" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button v-allow="'120101'" type="primary" @click="onSubmit">查询</el-button>
            <el-button v-allow="'120101'" @click="resetForm">重置</el-button>
            <el-button v-allow="'120103'" @click="handleExport">导出</el-button>
          </el-form-item>
        </el-form>
      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap" @refresh="refresh">
        <template slot="experimentTime" slot-scope="scope">
          <span>{{ $common.toDduration(scope.row.experimentTime) }}</span>
        </template>
        <template slot="type" slot-scope="scope">
          <span v-if="scope.row.type == 0">学生</span>
          <span v-if="scope.row.type == 1">老师</span>
        </template>
        <template slot="studentRealName" slot-scope="scope">
          <span>{{
            scope.row.studentCode + "-" + scope.row.studentRealName
          }}</span>
        </template>
        <template slot="status" slot-scope="scope">
          <span class="nx-del" v-if="scope.row.status == 0">进行中</span>
          <span class="nx-success" v-if="scope.row.status == 1">已完成</span>
        </template>
        <template slot="commentStatus" slot-scope="scope">
          <span class="nx-del" v-if="scope.row.commentStatus == 0">未批阅</span>
          <span class="nx-success" v-if="scope.row.commentStatus == 1">已批阅</span>
        </template>
        <template slot="operation" slot-scope="scope">
          <span v-allow="'120102'" v-if="scope.row.status == 1" @click="handleReport(scope.row)" class="nx-btn nx-edit">报告</span>
          <span v-allow="'120104'" v-if="scope.row.type == 0 && scope.row.commentStatus == 1" @click="handleReview(scope.row)" class="nx-btn nx-edit">批阅</span>
        </template>
      </NxTable>
    </div>
    <AddEditDialog v-if="typeDialog" :addEditId="addEditId"></AddEditDialog>
    <ReviewDialog :addEditId="addEditId" :addEditStatus="addEditStatus" :addEditIsDialog="reviewIsDialog"></ReviewDialog>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import { Page } from '@/utils/pagination'
import AddEditDialog from './components/addEditDialog.vue'
import ReviewDialog from './components/reviewDialog.vue'

import { getDataDeviceExperimentList, exportDataDeviceExperimentList } from '@/api/statistic'
import { getSchoolInfos } from '@/api/school'
import { getDeviceInfos } from '@/api/device'

export default {
  components: {
    NxTable,
    AddEditDialog,
    ReviewDialog
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        examinationRoomId: '',
        collegeId: '',
        facultyId: '',
        floorId: '',
        examinationRoomId: '',
        deviceId: '',
        type: '',
        startDate: '',
        endDate: '',
        status: '',
        commentStatus: ''
      },
      // 表格字段
      tableCols: [
        {
          label: '考场名称',
          prop: 'examinationRoomName',
          align: 'center',
          width: 150
        },
        {
          label: '学院名称',
          prop: 'collegeName',
          align: 'center',
          width: 150
        },
        {
          label: '院系名称',
          prop: 'facultyName',
          align: 'center',
          width: 150
        },
        {
          label: '楼层名称',
          prop: 'floorName',
          align: 'center'
        },
        {
          label: '教室名称',
          prop: 'classroomName',
          align: 'center',
          width: 150
        },
        {
          label: '设备名称',
          prop: 'deviceName',
          align: 'center',
          width: 150
        },
        {
          label: '实训名称',
          prop: 'deviceExperimentName',
          align: 'center',
          width: 150
        },
        {
          label: '实训类型',
          slot: 'type',
          align: 'center',
          width: 150
        },
        {
          label: '实训人',
          slot: 'studentRealName',
          align: 'center',
          width: 150
        },
        {
          label: '实训开始时间',
          prop: 'startTime',
          align: 'center',
          width: 180
        },
        {
          label: '实训结束时间',
          prop: 'endTime',
          align: 'center',
          width: 180
        },
        {
          label: '实训用时',
          slot: 'experimentTime',
          align: 'center',
          width: 150
        },
        {
          label: '实训状态',
          slot: 'status',
          align: 'center',
          width: 150
        },
        {
          label: '批阅状态',
          slot: 'commentStatus',
          align: 'center',
          width: 150
        },
        {
          label: '操作',
          slot: 'operation',
          align: 'center',
          width: 180,
          fixed: 'right'
        }
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: 'add',
      addEditId: '', // 添加编辑rowid
      treeId: [], // 考场组织id集合
      schoolArray: [], // 考场组织集合
      deviceArray: [], // 设备集合
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      date: [], // 日期范围
      userType: '', // 用户类型（0超级管理员，1考场管理员）
      examinationRoomId: '', // 考场id
      reviewIsDialog: false // 批阅弹窗显示状态
    }
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem('userType'))
    this.examinationRoomId = Number(window.localStorage.getItem('examinationRoomId'))
    if (this.userType == 1) {
      this.searchForm.examinationRoomId = this.examinationRoomId
    }

    // 查询
    this.getList()

    // 获取考场组织
    this.getSchoolInfos()
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        examinationRoomId: '',
        collegeId: '',
        facultyId: '',
        floorId: '',
        examinationRoomId: '',
        deviceId: '',
        studentCode: '',
        studentRealName: '',
        startDate: '',
        endDate: '',
        status: ''
      }

      if (this.userType == 1) {
        this.searchForm.examinationRoomId = this.examinationRoomId
      }

      // 清空
      this.treeId = []
      this.deviceArray = []
      this.date = []

      this.onSubmit()
    },
    // 查询列表
    getList() {
      getDataDeviceExperimentList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data
          this.pagination.count = res.count
        }
      })
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
      this.typeDialog = false
      this.reviewIsDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
      this.addEditIsDialog = false
      this.reviewIsDialog = false
    },
    // 实训报告
    handleReport(row) {
      this.crumbsStatus = '实训报告'
      this.addEditId = row.id + ''
      this.typeDialog = true
    },
    // 实训批阅
    handleReview(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'see'
      this.reviewIsDialog = true
    },
    // 获取考场组织
    getSchoolInfos() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 考场组织下拉切换
    changeTreeItem(val) {
      this.searchForm.examinationRoomId = this.userType == 1 ? this.examinationRoomId : ''
      this.searchForm.collegeId = ''
      this.searchForm.facultyId = ''
      this.searchForm.floorId = ''
      this.searchForm.examinationRoomId = ''
      this.searchForm.deviceId = ''
      this.deviceArray = []

      if (val) {
        let len = val.length
        let id = val[len - 1]
        if (len == 1) {
          this.searchForm.examinationRoomId = id
        } else if (len == 2) {
          this.searchForm.collegeId = id
        } else if (len == 3) {
          this.searchForm.facultyId = id
        } else if (len == 4) {
          this.searchForm.floorId = id
        } else if (len == 5) {
          this.searchForm.examinationRoomId = id
          // 获取设备列表
          this.getDeviceInfoList()
        }
        this.searchForm.examinationRoomId = val[0]
      }

      // 查询
      this.getList()
    },
    // 获取设备列表
    getDeviceInfoList() {
      if (!this.searchForm.examinationRoomId || this.searchForm.examinationRoomId == '') {
        return
      }

      getDeviceInfos({ id: this.searchForm.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.deviceArray = res.data
        }
      })
    },
    // 日期切换
    handleDate() {
      if (this.date && this.date.length == 2) {
        this.searchForm.startDate = this.date[0]
        this.searchForm.endDate = this.date[1]
      } else {
        this.searchForm.startDate = ''
        this.searchForm.endDate = ''
      }

      this.onSubmit()
    },
    //导出
    handleExport() {
      exportDataDeviceExperimentList(this.searchForm).then((res) => {
        if (res.code == 200) {
          window.open(res.data)
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>